package com.jy.appmarket.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

public class LocationUtil implements BDLocationListener {
	private static LocationUtil mInstance = null;
	private Context mContext = null;
	private LocationClient mLocationClient = null;
	private boolean isLocating = false;

	private String mLongitude;
	private String mLatitude;
	private String mCity;
	private static Handler sHandler = new Handler(Looper.getMainLooper());
	private static long INVALID_TIME = 1000 * 60 * 60 * 24;
	private long mLocationTime;

	private LocationUtil(Context ctx) {
		mContext = ctx.getApplicationContext();
	}

	public static LocationUtil getInstance(Context ctx) {
		if (mInstance == null) {
			mInstance = new LocationUtil(ctx);
		}
		return mInstance;
	}

	public void updateLocation() {

		if (!needUpdate()) {
			if (mLocationClient != null && mLocationClient.isStarted()) {
				mLocationClient.stop();
			}
			return;
		}
		if (isLocating) {
			return;
		}
		isLocating = true;
		mLocationTime = System.currentTimeMillis();
		if (Looper.myLooper() == Looper.getMainLooper()) {
			updateLocationInternal();
		} else {
			sHandler.post(new Runnable() {
				@Override
				public void run() {
					updateLocationInternal();
				}
			});
		}

	}

	private void updateLocationInternal() {

		mLocationClient = new LocationClient(mContext);
		mLocationClient.registerLocationListener(this);
		LocationClientOption option = new LocationClientOption();
		option.setIsNeedAddress(true);
		option.setIgnoreKillProcess(false);
		mLocationClient.setLocOption(option);
		mLocationClient.start();
	}

	private boolean needUpdate() {
		if (TextUtils.isEmpty(mLongitude) || TextUtils.isEmpty(mLatitude) || TextUtils.isEmpty(mCity)) {
			return true;
		}

		long currentTime = System.currentTimeMillis();
        return Math.abs(currentTime - mLocationTime) >= INVALID_TIME;
    }

	@Override
	public void onReceiveLocation(BDLocation location) {
		switch (location.getLocType()) {
		case BDLocation.TypeGpsLocation:
		case BDLocation.TypeNetWorkLocation:
		case BDLocation.TypeOffLineLocation:
			mLongitude = String.valueOf(location.getLongitude());
			mLatitude = String.valueOf(location.getLatitude());
			mCity = location.getCity();
			break;
		default:
			LogUtil.i("LocationUtil", "location error:" + location.getLocType());
			break;
		}
		if (mLocationClient != null) {
			mLocationClient.stop();
			mLocationClient.unRegisterLocationListener(this);
			mLocationClient = null;
		}
		isLocating = false;
	}

	// 获取经度坐标
	public String getLongitude() {
		updateLocation();
		return mLongitude;
	}

	// 获取纬度坐标
	public String getLatitude() {
		updateLocation();
		return mLatitude;
	}

	// 获取城市
	public String getCity() {
		updateLocation();
		return mCity;
	}

}